//
//  CreditNameViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/6/10.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "CreditNameViewController.h"

#import "AFNetworking.h"

@interface CreditNameViewController ()<UITextFieldDelegate>

@end

@implementation CreditNameViewController

-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";

    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"User" forKey:@"Model"];
    [parametersDic setObject:@"bind_bank_username" forKey:@"Action"];
    [parametersDic setObject:_creditNameTextField.text forKey:@"U_WithdrawalsName"];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        //        NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];

        UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:[dic objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];
        [alerView addAction:action];
        [self presentViewController:alerView animated:YES completion:nil];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"绑定银行姓名";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

//    // 状态栏(statusbar)
//    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
//
//    // 导航栏（navigationbar）
//    CGRect rectNav = self.navigationController.navigationBar.frame;

    UILabel *creditNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0+0+20, UISCREEN_WIDTH, 40)];
    creditNameLabel.text = @"      绑定姓名";
    creditNameLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:creditNameLabel];

    _creditNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, CGRectGetMinY(creditNameLabel.frame)+5, UISCREEN_WIDTH-120, 30)];
    _creditNameTextField.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
    //    _setUpMoneyPasswordTextField.clearButtonMode = YES; //清除模式开启
//    _creditNameTextField.secureTextEntry = YES; //密码输入框
    //tf.keyboardType = UIKeyboardTypeNumberPad; //键盘类型
    //协议执行者交给textField自己self
    _creditNameTextField.delegate = self;
    [self.view addSubview:_creditNameTextField];

    UIButton *creditNameButton = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(creditNameLabel.frame)+20, UISCREEN_WIDTH-20-20, 40)];
    [creditNameButton setTitle:@"设置" forState:UIControlStateNormal];
    [creditNameButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    creditNameButton.backgroundColor = [UIColor colorWithRed:36/255.0f green:11/255.0f blue:54/255.0f alpha:1];
    [creditNameButton addTarget:self action:@selector(screditNameAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:creditNameButton];

}

// 设置按钮点击事件
- (void)screditNameAction:(UIButton *)sender {
    [self obtainData];
}

@end
